package com.wwu.system.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.wwu.common.constant.CommConst;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;

/**
 *  系统用户实体
 * @author 一蓑烟雨
 * @date 2021-10-26
 * @version 1.0.0
 */
@Data
@Accessors(chain = true)
@TableName("sys_user")
@ApiModel(value="SysUser:系统用户实体", description="系统用户实体")
public class SysUser implements Serializable, UserDetails{
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value = "用户编码")
    @TableId(value = "user_id", type = IdType.ASSIGN_ID)
    private String userId;

    @ApiModelProperty(value = "用户名")
    @TableField("user_name")
    private String userName;

    @ApiModelProperty(value = "用户昵称")
    @TableField("nick_name")
    private String nickName;

    @ApiModelProperty(value = "登陆密码")
    @TableField("password")
    private String password;

    @ApiModelProperty(value = "用户头像")
    @TableField("user_avatar")
    private String userAvatar;

    @ApiModelProperty(value = "联系地址")
    @TableField("user_addr")
    private String userAddr;

    @ApiModelProperty(value = "联系邮箱")
    @TableField("user_email")
    private String userEmail;

    @ApiModelProperty(value = "联系电话")
    @TableField("user_phone")
    private String userPhone;

    @ApiModelProperty(value = "部门编码")
    @TableField("dept_id")
    private String deptId;

    @ApiModelProperty(value = "是否启用(0:可用；1:禁用)")
    @TableField("user_status")
    private Integer userStatus;

    @ApiModelProperty(value = "备注")
    @TableField("remark")
    private String remark;

    @ApiModelProperty(value = "拼音码")
    @TableField("py")
    private String py;

    @ApiModelProperty(value = "五笔码")
    @TableField("wb")
    private String wb;

    @ApiModelProperty(value = "删除状态(0:逻辑未删除；1:逻辑已删除)")
    @TableField("del_flag")
    @TableLogic
    private Integer delFlag;

    @ApiModelProperty(value = "最后登陆IP地址")
    @TableField("login_ip")
    private String loginIp;

    @ApiModelProperty(value = "最后登陆时间")
    /** 格式化后台和前端显示样式 */
    @DateTimeFormat(pattern = CommConst.DATE_TIME)
    @JsonFormat(pattern = CommConst.DATE_TIME,timezone = CommConst.TIME_ZONE)
    @TableField("login_time")
    private LocalDateTime loginTime;

    @ApiModelProperty(value = "创建人")
    @TableField(value = "creator",fill = FieldFill.INSERT)
    private String creator;

    @ApiModelProperty(value = "创建时间")
    /** 格式化后台和前端显示样式 */
    @DateTimeFormat(pattern = CommConst.DATE_TIME)
    @JsonFormat(pattern = CommConst.DATE_TIME,timezone = CommConst.TIME_ZONE)
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    @ApiModelProperty(value = "修改人")
    @TableField(value = "modifier",fill = FieldFill.INSERT_UPDATE)
    private String modifier;

    @ApiModelProperty(value = "修改时间")
    /** 格式化后台和前端显示样式 */
    @DateTimeFormat(pattern = CommConst.DATE_TIME)
    @JsonFormat(pattern = CommConst.DATE_TIME,timezone = CommConst.TIME_ZONE)
    @TableField(value = "modify_time",fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime modifyTime;

    @ApiModelProperty(value = "拥有的角色Id")
    /** 表中不存在该字段 */
    @TableField(exist = false)
    private String roleIds;

    @ApiModelProperty(value = "在线用户/登陆日志：会话id")
    /** 表中不存在该字段 */
    @TableField(exist = false)
    private String sessionId;

    @ApiModelProperty(value = "在线用户/登陆日志：浏览器")
    /** 表中不存在该字段 */
    @TableField(exist = false)
    private String browser;

    @ApiModelProperty(value = "在线用户/登陆日志：操作系统")
    /** 表中不存在该字段 */
    @TableField(exist = false)
    private String os;

    /**
     *  重写UserDetails类的实现方法，用来获取当前用户的角色
     * @author 一蓑烟雨
     * @date 2021/11/21 0021 上午 9:38
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //获取实体类中的roles属性，构造SimpleGrantedAuthority返回
//        List<SimpleGrantedAuthority> authorities = roles
//                .stream()
//                .map(sysRole -> new SimpleGrantedAuthority(sysRole.getRoleName()))
//                .collect(Collectors.toList());
        return null;
    }

    /**
     *  重写UserDetails类的实现方法，获取登陆用户名
     * @author 一蓑烟雨
     * @date 2021/11/21 0021 上午 9:42
     */
    @Override
    public String getUsername() {
        return this.userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.userStatus==0 ? true : false;
    }

    /**
     * 重写equals方法，并发登陆时判断是否是同一个账号
     * @param obj
     * @return boolean
     * @author 一蓑烟雨
     * @date 2023/7/26 9:34
     */
    @Override
    public boolean equals(Object obj) {
        return obj instanceof SysUser ? this.userName.equals(((SysUser) obj).userName) : false;
    }

    /**
     * 重写hashCode方法，并发登陆时判断是否是同一个账号
     * @return int
     * @author 一蓑烟雨
     * @date 2023/7/26 9:36
     */
    @Override
    public int hashCode() {
        return this.userName.hashCode();
    }

}
